package eerbaar.expression.internal.lib;

import eerbaar.area.IArea;
import eerbaar.context.IContext;
import eerbaar.context.IParams;
import eerbaar.context.ParamFactory;
import eerbaar.elements.IElement;
import eerbaar.game.IGame;
import eerbaar.persona.IPersona;

public class PetOnElementExpression extends AbstractExpression {

    private static final String[] paramNames = new String[] { "name" };

    private IParams params;

    private String target;

    public PetOnElementExpression() {
        super("petOnElement");
        this.params = ParamFactory.createParams();
    }

    public PetOnElementExpression(IParams params) {
        super("petOnElement");
        this.params = params;

        setup(params);
    }

    @Override
    public boolean execute(IContext model) {
        assert (this.target != null);

        int c = 0;
        IGame game = (IGame) model;
        IArea area = game.getArea();
        IPersona pet = game.getPersona("pet");
        IElement[] elements = area.getElements(pet.getPosition());
        for (IElement element : elements) {
            if (element.getName().equals(this.target)) {
                c++;
            }
        }

        return (c > 0);
    }

    @Override
    public String[] getParamNames() {

        return paramNames;
    }

    @Override
    public IParams getParams() {

        return this.params;
    }

    public String getTarget() {
        return this.target;
    }

    @Override
    public void setParams(IParams params) {
        this.params = params;
        setup(params);
    }

    public void setTarget(String target) {
        this.target = target;
    }

    private void setup(IParams params) {

        this.target = params.get("name");
    }
}
